<?php namespace Admin\Model;

use Hdphp\Model\Model;

/**
 * 商品属性管理模型
 */
class GoodsAttr extends Model{

	//数据表
	protected $table = "shop_goods_attr";

	//完整表名
	protected $full = false;

	//自动验证
	protected $validate=array(
	);

	//自动完成
	protected $auto=array(
	);

	//商品属性保存的模型方法
	public function addGoodsAttr($goods_id)
	{
		foreach ($_POST['attr_id'] as $key => $value) 
		{
			$data['hd_shop_goods_goods_id'] = $goods_id;
			$data['attr_value'] = $_POST['attr_value'][$key];
			$data['hd_shop_attr_attr_id'] = $_POST['attr_id'][$key];
			$data['add_price'] = (int)$_POST['add_price'][$key];
			$this->add($data);
		}
		return true;
	}

	//编辑
	public function editGoodsAttr($goods_id)
	{
		//空数组
		$data = array();
		//遍历post数据  生成键与值
		foreach ($_POST['attr_id'] as $key => $value)
		{
			//去空操作
			$_POST['attr_value'][$key] = trim($_POST['attr_value'][$key]);
			//如果是空的就操作 针对普通属性
			if(empty($_POST['attr_value'][$key])){
				continue;
			}
			//组合数据
			$arr['attr_value'] = $_POST['attr_value'][$key];
			$arr['add_price'] = (int)$_POST['add_price'][$key];
			$arr['hd_shop_goods_goods_id'] = $goods_id;
			$arr['hd_shop_attr_attr_id'] = $_POST['attr_id'][$key];
			//组合条件数据
			$where = array(
					'hd_shop_goods_goods_id' => $goods_id,
					'and hd_shop_attr_attr_id' => $arr['hd_shop_attr_attr_id'],
					'and attr_value' => $arr['attr_value'],
				);
			//利用条件数据，查找数据库判断是否有主键返回
			$goods_attr_id=Db::table('shop_goods_attr')->where($where)->pluck('goods_attr_id');
			//有返回值得就，将主键组合到数组
			if($goods_attr_id){
				$arr['goods_attr_id'] = $goods_attr_id;
			}
			//组合出来的数组 压入预先的data
			$data[] = $arr;
		}
		//先把数据库清除
		Db::table('shop_goods_attr')->where('hd_shop_goods_goods_id',$goods_id)->delete();
		//遍历，添加进去， 因为数据里面规格属性有主键，这里是手动设置主键。
		foreach ($data as $value) 
		{
			$this->add($value);
		}
		return true;
	}


	//根据类型遍历需要的模版  规格有加价，普通属性无加价
	public function getListTpl($type_id)
	{
		//实例Db类  拿数据
		$attrs = Db::table('shop_attr')->where('hd_shop_type_id',$type_id)->get();
		$html="";
		foreach($attrs as $v){
			switch ($v['attr_type']) {
				case 1:
					$priceTag = '<input type="hidden" name="add_price[]" value="0">';
					break;
				
				case 2:
					$priceTag = '<input type="text" name="add_price[]"><span class="add-price" onclick="addP(this)">＋</span>';
					break;
			}
			switch ($v['show_type']) {
				case 1:					
					$show = <<<php
<tr>
	<th style="width:300px;text-align: rigth;">{$v['attr_name']}</th>
	<td>
		<input type="hidden" name="attr_id[]" value="{$v['attr_id']}">
		<input type="text" name="attr_value[]">
		$priceTag
	</td>
</tr>
php;
					break;
				
				case 2:
					$a = preg_split('/\n/', $v['attr_values']);
					$option = "";
					foreach ($a as $value) {
						$option .= '<option value="'.$value.'">'.$value.'</option>';
					}
					$show = <<<php
<tr>
	<th style="width:300px;text-align: rigth;">{$v['attr_name']}</th>
	<td>
		<input type="hidden" name="attr_id[]" value="{$v['attr_id']}">
		<select name="attr_value[]">
		<option value="">==请选择==</option>
		$option
		</select>
		$priceTag
	</td>
</tr>
php;
					break;
			}
			$html .= $show;
		}
		return $html;
	}

	//编辑页面调用的方法，传入两个参
	public function editListTpl($type_id,$goods_id)
	{
		$sql ="SELECT * FROM hd_shop_attr LEFT JOIN ((SELECT * FROM hd_shop_goods_attr WHERE hd_shop_goods_goods_id=$goods_id) AS sga) ON hd_shop_attr_attr_id = attr_id WHERE hd_shop_type_id = $type_id";
		$attrs = Db::select($sql);
		$html='';
		foreach ($attrs as $v) 
		{
			switch ($v['attr_type']) {
				case 1:
					$priceTag = '<input type="hidden" name="add_price[]" value="0">';
					break;
				
				case 2:
					$priceTag = '<input type="text" name="add_price[]" value="'.$v['add_price'].'"><span class="add-price" onclick="addP(this)">＋</span>';
					break;
			}

			switch ($v['show_type']) {
				case 1:					
					$show = <<<php
<tr>
	<th style="width:300px;text-align: rigth;">{$v['attr_name']}</th>
	<td>
		<input type="hidden" name="attr_id[]" value="{$v['attr_id']}">
		<input type="text" name="attr_value[]" value="{$v['attr_value']}">
		$priceTag
	</td>
</tr>
php;
					break;
				
				case 2:
					$a = preg_split('/\n/', $v['attr_values']);
					$option = "";					
					foreach ($a as $value) {
						$value = rtrim($value);
						$v['attr_value'] = rtrim($v['attr_value']);
						$selected = $value==$v['attr_value']?"selected":'';
						$option .= '<option value="'.$value.'" '.$selected.'>'.$value.'</option>';
						
					}
					$show = <<<php
<tr>
	<th style="width:300px;text-align: rigth;">{$v['attr_name']}</th>
	<td>
		<input type="hidden" name="attr_id[]" value="{$v['attr_id']}">
		<select name="attr_value[]">
		<option value="">==请选择==</option>
		$option
		</select>
		$priceTag
	</td>
</tr>
php;
					break;
			}
			$html .= $show;
		}
		return $html;
	}
}